//
//  yxpPickerImage.m
//  TestProject
//
//  Created by 杨小平 on 14-8-23.
//  Copyright (c) 2014年 yangxiaoping. All rights reserved.
//

#import "yxpPickerImage.h"

@interface yxpPickerImage ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIImageView *_imageView;
    
}


@end

@implementation yxpPickerImage

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)initView
{
    _imageView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 74, 300, self.view.frame.size.height-124)];
    _imageView.layer.borderColor=[[UIColor skyBlueColor] CGColor];
    _imageView.layer.borderWidth=1.0f;
    [self.view addSubview:_imageView];
    
    UIButton *button1=[[UIButton alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height-40, 145, 30)];
    button1.layer.borderWidth=1.0;
    button1.tag=99;
    button1.layer.borderColor=[[UIColor skyBlueColor]  CGColor];
    [button1 setTitle:@"从相册选取" forState: UIControlStateNormal];
    [button1 setTitleColor:[UIColor skyBlueColor] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(pickerImageWithButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton *button2=[[UIButton alloc] initWithFrame:CGRectMake(165, self.view.frame.size.height-40, 145, 30)];
    button2.layer.borderWidth=1.0;
    button2.tag=88;
    button2.layer.borderColor=[[UIColor skyBlueColor]  CGColor];
    [button2 setTitle:@"现在拍照" forState: UIControlStateNormal];
    [button2 setTitleColor:[UIColor skyBlueColor] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(pickerImageWithButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
}
- (void)pickerImageWithButton:(UIButton *)button
{
    if (button.tag==99) {
        [self acquireImageWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }else if(button.tag==88)
    {
        [self acquireImageWithSourceType:UIImagePickerControllerSourceTypeCamera];
    }
}

- (void)acquireImageWithSourceType:(UIImagePickerControllerSourceType)sourceType
{
    UIImagePickerController *imagePicker=[[UIImagePickerController alloc] init];
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    //获取途径
    imagePicker.sourceType = sourceType;
    //
    imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    //是否编辑图片
    imagePicker.allowsEditing = NO;
    
    [self presentViewController:imagePicker animated:YES completion:nil];
}

#pragma mark -- UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //得到原图
    UIImage *image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    _imageView.image=image;
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	
    [self initView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
